package com.ysh.jackson;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;

/*
    JSON转换工具的使用
        SpringMVC转换默认使用Jackson

 */
public class ObjectMapperTest {
    private ObjectMapper mapper = new ObjectMapper();

    /*对象类型：JSON与对象互转
        1. User对象转JSON，JSON转User对象
            json字符串 = {"name":"张三","age":23}
            user对象 = User{name="张三",age=23}
     */
    @Test
    public void test01() throws Exception {
        // User对象转json
        User user = new User("张三", 23);
        String json = mapper.writeValueAsString(user);
        System.out.println("json字符串：" + json);

        //json转User对象
        User user2 = mapper.readValue(json, User.class);
        System.out.println("java对象 = " + user);
        /*
            json字符串：{"name":"张三","age":23}
            java对象 = User{name='张三', age=23}
         */
    }

    /*集合类型：JSON和集合互转
        2. map<String,String>转json，json转map<String,String>
            json字符串 = {"姓名":"张三","性别":"男"}
            map对象 = {姓名=张三，性别=男}
     */
    @Test
    public void test02() throws Exception {
        // map<String,String>转json
        HashMap<String, String> map = new HashMap<>();
        map.put("姓名", "张三");
        map.put("性别", "男");
        String json = mapper.writeValueAsString(map);
        System.out.println("json字符串：" + json);

        // json转map<String,String>
        HashMap<String,String> map2 = mapper.readValue(json,HashMap.class);
        System.out.println("java对象：" + map2);
    }

    /*Map集合内含对象与json互转
        3. Map<String,User>转json，json转Map<String, User>
            json = {"一班":{"name":"张三","age":23},"二班":{"name":"李四","age":24}}
            map2 = {一班=User{name='张三', age=23}, 二班=User{name='李四', age=24}}
     */
    @Test
    public void test03() throws Exception{
        // Map<String,User>转json
        HashMap<String,User> map = new HashMap<>();
        map.put("一班",new User("张三",23));
        map.put("二班",new User("李四",24));
        String json = mapper.writeValueAsString(map);
        System.out.println("json = " + json);

        // json转map<String,User>
        HashMap<String,User> map2 = mapper.readValue(json, new TypeReference<HashMap<String,User>>(){});
        System.out.println("map2 = " + map2);
    }

    /*List集合与json互转
        4.List<String>转json,json转List<String>
            json = ["张三","李四"]
            list2 = [张三, 李四]
     */
    @Test
    public void test04() throws Exception{
        // List<String>转json
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        String json = mapper.writeValueAsString(list);
        System.out.println("json = " + json);

        // json转List<String>
        ArrayList<String> list2 = mapper.readValue(json,ArrayList.class);
        System.out.println("list2 = " + list2);
    }

    /*存有json的List与JSON互转
        5. List<User>转json，json转List<User>
            json = [{"name":"张三","age":23},{"name":"李四","age":24}]
            list2 = [User{name='张三', age=23}, User{name='李四', age=24}]
     */
    @Test
    public void test05() throws Exception{
        // List<User>转json
        ArrayList<User> list = new ArrayList<>();
        list.add(new User("张三",23));
        list.add(new User("李四",24));
        String json = mapper.writeValueAsString(list);
        System.out.println("json = " + json);

        // json转List<User>
        Object list2 = mapper.readValue(json, new TypeReference<ArrayList<User>>() {});
        System.out.println("list2 = " + list2);
    }
}
